package com.hongxun.design.adapters;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import java.util.List;

import com.hongxun.design.R;
import com.hongxun.design.bean.photopassbean.PhotoPassIconBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.utils.InputUtil;
import com.hongxun.design.utils.PopwindowUtil;

/**
 * Created by zerdoor_pc on 2016/4/29.
 */
public class PPassDialogAdapter extends BaseAdapter {

    private List<PhotoPassIconBean> beanList;
    LayoutInflater inflater;
    Activity context;
    View parent;
    public PPassDialogAdapter(List<PhotoPassIconBean> beanList, Activity context,View parent) {
        this.beanList = beanList;
        this.context = context;
        this.parent = parent;
        inflater = LayoutInflater.from(context);
        MyLog.L("listdatalistdatalistdata：" + JSON.toJSONString(beanList));
    }

    public List<PhotoPassIconBean> getData() {
        return beanList;
    }

    public void update() {
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public int getItemViewType(int position) {
        int type = Constant.EdItem;
        switch (beanList.get(position).getType()) {
            case Constant.EdItem:
                type = Constant.EdItem;
                break;
            case Constant.ChoiceItem:
                type = Constant.ChoiceItem;
                break;
        }
        return type;
    }

    private int index = -1;

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        int ViewType = getItemViewType(position);
        EditTextHolder editTextHolder = null;
        ChoiceHolder choiceHolder = null;
        switch (ViewType) {
            case Constant.EdItem:
                convertView = inflater.inflate(R.layout.listview_item_photo_ed_dialog, null);
                editTextHolder = new EditTextHolder(convertView);
                convertView.setTag(editTextHolder);
                break;
            case Constant.ChoiceItem:
                convertView = inflater.inflate(R.layout.listview_item_photo_choice_dialog, null);
                choiceHolder = new ChoiceHolder(convertView);
                convertView.setTag(choiceHolder);
                break;

        }
        try {
            PhotoPassIconBean bean;
            switch (ViewType) {
                case Constant.EdItem:
                    bean = beanList.get(position);
                    editTextHolder.photo_ed_title.setText(bean.getTitle());
                    if (bean.getValue().length() == 0) {
                        editTextHolder.photo_ed_write.setHint(bean.getNovalue());
                    } else {
                        editTextHolder.photo_ed_write.setText(bean.getValue());
                    }
                    editTextHolder.photo_ed_write.addTextChangedListener(bean.getTextWatcher());
                    editTextHolder.photo_ed_write.setInputType(bean.getInputType());
                    final int mposition = position;
                    editTextHolder.photo_ed_write.setOnTouchListener(new View.OnTouchListener() {
                        public boolean onTouch(View view, MotionEvent event) {
                            if (event.getAction() == MotionEvent.ACTION_UP) {
                                index = mposition;
                            }
                            return false;
                        }
                    });
                    editTextHolder.photo_ed_write.clearFocus();
                    if (index != -1 && index == position) {
                        // 如果当前的行下标和点击事件中保存的index一致，手动为EditText设置焦点。
                        editTextHolder.photo_ed_write.requestFocus();
                    }
                    break;
                case Constant.ChoiceItem:
                    // choiceHolder
                    bean = beanList.get(position);
                    choiceHolder.title.setText(bean.getTitle());
                    if (bean.getValue().length() == 0) {
                        choiceHolder.value.setText(bean.getNovalue());
                    } else {
                        choiceHolder.value.setText(bean.getValue());
                    }
                    choiceHolder.item.setOnClickListener(new MyOnClickListener(bean, choiceHolder.value) {
                        @Override
                        public void onClick(View view, PhotoPassIconBean bean, TextView textView) {
                            InputUtil.closeInput(context);
                            WindowManager.LayoutParams params = context.getWindow().getAttributes();
                            params.alpha = 0.7f;
                            context.getWindow().setAttributes(params);
                            PopwindowUtil.popChoice_PhotoDialog(context, textView, bean,parent);
                        }
                    });
                    break;

            }
            return convertView;

        } catch (Exception e) {
            return null;
        }


    }


    class EditTextHolder {
        public TextView photo_ed_title, photo_ed_right;
        public EditText photo_ed_write;

        EditTextHolder(View convertView) {
            photo_ed_title = (TextView) convertView.findViewById(R.id.photo_ed_title_dialog);
            photo_ed_write = (EditText) convertView.findViewById(R.id.photo_ed_write_dialog);
            photo_ed_right = (TextView) convertView.findViewById(R.id.photo_ed_right_dialog);

        }
    }

    class ChoiceHolder {
        public TextView title, value, right;
        RelativeLayout item;

        ChoiceHolder(View convertView) {
            item = (RelativeLayout) convertView.findViewById(R.id.photo_choice_item_dialog);
            title = (TextView) convertView.findViewById(R.id.photo_choice_title_dialog);
            value = (TextView) convertView.findViewById(R.id.photo_choice_value_dialog);
            right = (TextView) convertView.findViewById(R.id.photo_choice_right_dialog);
            SetTypeface.set(right, IconSourse.DROPDOWN);
        }
    }




    abstract class MyOnClickListener implements View.OnClickListener {

        private PhotoPassIconBean bean;
        TextView textView;

        MyOnClickListener(PhotoPassIconBean bean, TextView textView) {
            this.textView = textView;
            this.bean = bean;
        }

        @Override
        public void onClick(View view) {
            onClick(view, bean, textView);
        }

        public abstract void onClick(View view, PhotoPassIconBean bean, TextView textView);
    }


}
